<?php 
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

class ZticketContentObject extends ObjectModel
{
	/** @var string SQL Table name */
	protected 	$table ='zhelpdesk_ticket_content';
	/** @var string SQL Table identifier */
	protected 	$identifier = 'id_zhelpdesk_ticket_content';
	
	/** @var integer Object id */
    public      $id_zhelpdesk_ticket_content;  
    /** @var integer Object id department */
    public      $id_zhelpdesk_ticket;  
	/** @var integer Object staff */
	public 		$id_staff;
	/** @var tinytext Object subject */
	public      $content;
	/** @var datetime Object date add */
	public      $date_add;
	/** @var integer Object files attached */
	public      $attached_files;
	/** @var integer Object id ticket priovity */
	public      $id_zhelpdesk_ticket_rating;
	
	/** @var array Fields validity functions for admin panel forms */
	protected $fieldsValidate = array(
		'id_zhelpdesk_ticket_content' 	 			=> 'isInt',
		'id_zhelpdesk_ticket' 					=> 'isInt',
		
	);
	/**
	 * Build object
	 *
	 * @param integer $id Existing object id in order to load object (optional)
	 * @param integer $id_lang Required if object is multilingual (optional)
	 */
	public function  __construct($id = null, $lang = null)
	{
		parent::__construct($id, $lang);
	}
	/**
	 * @see ObjectModelCore::getFields()
	 * 
	 * Prepare fields for ObjectModel class (add, update)
	 * All fields are verified (pSQL, intval...)
	 *
	 * @return array All object fields
	 */
	public function getFields()
	{
		parent::validateFields();
		$fields['id_zhelpdesk_ticket_content'] = (int)($this->id_zhelpdesk_ticket_content);
		$fields['id_zhelpdesk_ticket'] = (int)($this->id_zhelpdesk_ticket);
		$fields['id_staff'] = (int)($this->id_staff);
		$fields['content'] = pSQL($this->content, true);
		$fields['date_add'] = pSQL($this->date_add);
		$fields['attached_files'] = pSQL($this->attached_files);
		$fields['id_zhelpdesk_ticket_rating'] = (int)($this->id_zhelpdesk_ticket_rating);
		return $fields;
	}
	
	/**
	 * @see ObjectModel->makeTranslationFields
	 * 
	 * make Translation Fields
	 * @param array $fields
	 * @param array $fieldsArray
	 * @param integer $id_language
	 */
	protected function makeTranslationFields(&$fields, &$fieldsArray, $id_language)
	{
		$fields[$id_language]['id_lang'] = $id_language;
		$fields[$id_language][$this->identifier] = (int)($this->id);
		foreach ($fieldsArray as $field)
		{
			/* Check fields validity */
			if (!Validate::isTableOrIdentifier($field))
				die(Tools::displayError());

			/* Copy the field, or the default language field if it's both required and empty */
			if ((!$this->id_lang AND isset($this->{$field}[$id_language]) AND !empty($this->{$field}[$id_language])) 
			OR ($this->id_lang AND isset($this->$field) AND !empty($this->$field)))
				$fields[$id_language][$field] = $this->id_lang ? pSQL($this->$field) : pSQL($this->{$field}[$id_language], true);
			elseif (in_array($field, $this->fieldsRequiredLang))
				$fields[$id_language][$field] = $this->id_lang ? pSQL($this->$field) : pSQL($this->{$field}[Configuration::get('PS_LANG_DEFAULT')], true);
			else
				$fields[$id_language][$field] = '';
		}
	}
	/**
	 * 
	 * Get content ticket creat new
	 * @param int $id_ticket
	 * @return text
	 * 
	 */
	public static function getContentNewTicket ($id_ticket) {
		$rs = Db::getInstance()->getRow('SELECT content,MIN(id_zhelpdesk_ticket_content)  FROM '._DB_PREFIX_.'zhelpdesk_ticket_content
										WHERE id_zhelpdesk_ticket='.$id_ticket);
		return htmlentities($rs['content']); 
	}
	
	/**
	 * 
	 * Get content ticket miss
	 * @param int $id_ticket
	 * @return text
	 */
	public static function getContentTicketMiss ($id_ticket) {
		$rs = Db::getInstance()->getRow('SELECT MAX(id_zhelpdesk_ticket_content)
										FROM '._DB_PREFIX_.'zhelpdesk_ticket_content
										WHERE id_zhelpdesk_ticket='.$id_ticket);
		$content = Db::getInstance()->getRow('SELECT content
										FROM '._DB_PREFIX_.'zhelpdesk_ticket_content
										WHERE id_zhelpdesk_ticket_content='.$rs['MAX(id_zhelpdesk_ticket_content)']);
		return $content['content']; 
	}
	
}
?>